package com.xzit.headline.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.xzit.headline.fragment.sub.Page1Fragment;
import com.xzit.headline.fragment.sub.Page2Fragment;
import com.xzit.headline.fragment.sub.Page3Fragment;
import com.xzit.headline.fragment.sub.Page4Fragment;

import java.util.ArrayList;
import java.util.List;


//3.定义适配器
public class MyPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> pages = new ArrayList<>();
    private List<String> titles = new ArrayList<>();

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        pages.add(new Page1Fragment());//0
        pages.add(new Page2Fragment());//1
        pages.add(new Page3Fragment());//2
        pages.add(new Page4Fragment());//3
        titles.add("Page1Fragment");
        titles.add("Page2Fragment");
        titles.add("Page3Fragment");
        titles.add("Page4Fragment");
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {//返回页面内容
        return pages.get(position);
    }

    @Override
    public int getCount() {//返回页面数量
        return pages.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}

